package com.jy.api.controller.clerk;

import com.jy.api.dao.CouponInstanceDao;
import com.jy.api.domain.CouponInstance;
import com.jy.api.exception.MyException;
import com.jy.api.service.CouponInstanceService;
import com.jy.api.service.WechatService;
import com.jy.api.signUpReq.clerk.CouponInstanceSignUpReq;
import com.jy.api.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.security.Principal;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by lihu on 2017/11/17.
 */
@Slf4j
@RestController
@RequestMapping(value = "v1/clerk", produces = "application/json;charset=utf-8")
public class ClerkCouponInstanceController {

    @Autowired
    CouponInstanceService couponInstanceService;

    @Autowired
    WechatService wechatService;

    @Autowired
    CouponInstanceDao couponInstanceDao;


    /**
     * 优惠卷发放
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/coupon-instance")
    public ResponseEntity couponInstance(@Valid @RequestBody CouponInstanceSignUpReq instanceSignUpReq,
                                         BindingResult bindingResult,
                                         Principal principal) {
        try {
            if (bindingResult.hasErrors()) {
                log.error("【优惠卷】参数不正确, instanceSignUpReq={}", instanceSignUpReq);
                return new ResponseEntity(bindingResult.getFieldError().getDefaultMessage(), HttpStatus.BAD_REQUEST);
            }
            String couponInstanceId = couponInstanceService.addCouponInstance(instanceSignUpReq, principal);

            Map map = new HashMap();
            map.put("couponInstanceId", couponInstanceId);
            map.put("url", wechatService.getCouponInstanceUrl(couponInstanceId));
            return new ResponseEntity(map, HttpStatus.OK);
        } catch (MyException ex) {
            log.error("allOrder is error==> instanceSignUpReq:{}", instanceSignUpReq, ex);
            return new ResponseEntity(ex.getMessage(), HttpStatus.BAD_REQUEST);
        } catch (Exception ex) {
            log.error("allOrder is error==> instanceSignUpReq:{}", instanceSignUpReq, ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 优惠卷轮询领取结果
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/{couponInstanceId}/query-instance")
    public ResponseEntity queryInstance(@PathVariable String couponInstanceId) {
        try {
            CouponInstance couponInstance = couponInstanceDao.findOne(couponInstanceId);
            if (couponInstance == null) {
                return new ResponseEntity("查询错误！优惠卷信息不存在！", HttpStatus.BAD_REQUEST);
            }

            if (StringUtils.isNotBlank(couponInstance.getOwnerId())) {
                return new ResponseEntity(new Result().OK(""), HttpStatus.OK);
            } else {
                return new ResponseEntity(HttpStatus.ACCEPTED);
            }
        } catch (Exception ex) {
            log.error("queryInstance is error==> couponInstanceId:{}", couponInstanceId, ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }


}
